export class Promisor<T> {

    promise: Promise<T>;
    declare resolve: (value: T | PromiseLike<T>) => void;
    declare reject: (reason?: any) => void;

    constructor() {
        this.promise = new Promise<T>((resolve_, reject_) => {
            this.resolve = resolve_;
            this.reject = reject_;
        });
    }

    /**
     * 创建
     */
    static create<T>(): Promisor<T> {
        return new Promisor<T>();
    }

    /**
     *  sleep
     * @param ms 毫秒
     */
    static sleep(ms: number): Promise<void> {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    /**
     * 创建一个延迟后兑现给定值的 Promise
     * @param value
     * @param delay
     */
    static defer<T>(value: T, delay: number): Promise<T> {
        return new Promise<T>(resolve => setTimeout(() => resolve(value), delay));
    }
}